/**
 * Application Name: TCSS422_WebCrawler
 * Group Name: The Other Guys
 * Members: Scott Freeman, Anthony Melcher, Jason Green
 * Date: November 10, 2011
 * 
 * Related libraries: Jericho HTML Page Parser (author unknown) 
 * 					  http://jericho.htmlparser.net/docs/index.html
 */
package controller;

import java.net.URI;
import java.net.URISyntaxException;
import report.DataGatherer;

import jericho_parser_wrapper.PageParser;
import jericho_parser_wrapper.PageRetriever;

/**
 * This class represents the single threaded 
 * version to crawl the Web retrieving web
 * pages, parsing the pages, and collecting
 * data about them.
 */
public class SingleThread {
	
	/**
	 * The root seed page url address.
	 */
	private String url;

	/**
	 * Instantiates a SingleThread object.
	 * 
	 * @param seed the root URL to start from
	 */
	public SingleThread(final String seed) {
		url = seed;
	}
	
	/**
	 * This method starts the web crawler. It handles
	 * getting new HTML pages, parsers the pages, collects
	 * data, and reports the data.
	 * @throws URISyntaxException if the seed URL is in a invalid format
	 */
	public void execute() throws URISyntaxException {
		final DataGatherer dataGatherer = new DataGatherer();
		
		Spider.urlBuffer.add(new URI(url));
		
		while(Spider.processedPages.get() < Spider.maxPages.get()) {
		
			if(Spider.urlBuffer.size() > 0) {
				new PageRetriever().run();			
			}
			
			if(Spider.pageBuffer.size() > 0) {
				new PageParser().run();
				Spider.processedPages.getAndIncrement();
			}

			dataGatherer.run();
		}
	}
}
